package com.manage.server.controller.admin;

import com.auth0.jwt.JWT;
import com.manage.dto.UserLoginDTO;
import com.manage.entity.User;
import com.manage.result.Result;
import com.manage.server.service.UserService;
import com.manage.utils.JwtUtils;
import com.manage.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class UserLoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.login(userLoginDTO.getUsername());
        if(user==null||!user.getPassword().equals(userLoginDTO.getPassword())){
            return Result.error("账号或密码错误");
        }
        JwtUtils jwtUtils = new JwtUtils();
        String token = jwtUtils.getSecretKey(user);
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user, userLoginVO);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
    }

}
